package com.ruoyi.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.api.vo.TrackBean;
import com.ruoyi.api.vo.VehicleTrack;
import com.ruoyi.util.HttpClientUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Author: lvaitly
 * Time: 15:17
 * Desc: 移动端调后台页面
 */
@Controller
@RequestMapping("/api/web")
public class WebPageController {
    @Value("${wspaas.wsVehicleUrl}")
    private String vehicleUrl;

    @SneakyThrows
    @GetMapping("/{service}/{model}/{function}")
    public String gotoMap(@PathVariable String service, @PathVariable String model,@PathVariable String function, String data, ModelMap mmap)
    {
        StringBuilder sb=new StringBuilder();
        if("bdmap_animation".equalsIgnoreCase(function)){
            sb.append(vehicleUrl).append("/KeChuangVehicle.asmx/GetVehicleTrackRT");
            String str= new String(Base64Utils.decode(data.getBytes("utf-8")));
            JSONObject o=JSON.parseObject(str);
            String s = HttpClientUtil.doPost(sb.toString(), o);
            if (!"-1".equalsIgnoreCase(s)) {
                JSONObject result = JSON.parseObject(s);
                if (null != result && 0 == result.getInteger("code")) {//成功
                    if (null != result.getJSONArray("data")) {
                        List<VehicleTrack> vts = JSON.parseArray(result.getJSONArray("data").toJSONString(), VehicleTrack.class);
                        if(null!=vts&&!vts.isEmpty()){
                            VehicleTrack v=vts.get(0);
                            sb.setLength(0);
                            sb.append("①.车辆:").append(v.getCarNo()).append("(").append(v.getCarType()).append(")<br/>").append("②.时间:").append(v.getReportDate()).append("<br/>");
                            List<TrackBean> track = v.getTrack();
                            if(null!=track&&!track.isEmpty()){
                                TrackBean tb=track.get(0);
                                sb.append("③.时速:").append(tb.getSpeed()).append("km/h");
                                mmap.put("msg",sb.toString());
                                mmap.put("lat",tb.getLatitude());
                                mmap.put("lng",tb.getLongitude());
                                return "system/vehicle/"+function;
                            }
                        }
                    }
                }
            }
            mmap.put("lng",114.224693);
            mmap.put("lat",22.72769);
            mmap.put("msg","车辆没有信号,请联系相关人员");
            return "system/vehicle/bdmap_bounce";
        }else {
            sb.append(service).append("/").append(model).append("/").append(function);
            mmap.put("mdata",data);
            return sb.toString();
        }
    }
}
